package org.windowkit.toolkit.widget;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * @author Rich
 */
public class StatusBar extends JComponent {



    /**
	 *
	 */
	private static final long serialVersionUID = 8921300896926346756L;

	private final JPanel[] panels;

    /** Displays the current message */
    private final JLabel message;


    /** Store the text for the capture/reset functionality */
    private String savedText;

    private final int segmentCount;



    private static final int DEFAULT_HEIGHT = 23;


    public StatusBar(){
        this(DEFAULT_HEIGHT, null, null);
    }

    public StatusBar(final int height){
        this(height, null, null);
    }

    public StatusBar(final JComponent[] leftSlots, final JComponent[] rightSlots) {
        this(DEFAULT_HEIGHT, leftSlots, rightSlots);
    }

    public StatusBar(final int height, final JComponent[] leftSlots, final JComponent[] rightSlots) {
        super();


        segmentCount = (leftSlots  != null ? leftSlots.length  : 0)
                     + (rightSlots != null ? rightSlots.length : 0)
                     + 1;

        panels = new JPanel[segmentCount];

        int cursor = 0;
        if(leftSlots != null && leftSlots.length > 0){

            for( ; cursor < leftSlots.length; cursor++){

                panels[cursor] = createComponent(leftSlots[cursor]);
            }
        }

        message = new JLabel();
        int msgPanelIndex = cursor;
        panels[cursor++] = createComponent(message);

        if(rightSlots != null && rightSlots.length > 0){

            for (JComponent rightSlot : rightSlots) {

                panels[cursor++] = createComponent(rightSlot);
            }
        }


        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weighty = 1.0;
        gbc.weightx = 0.0;
        add(Box.createVerticalStrut(height), gbc);

        for(int i = 0; i < segmentCount; i++){
            gbc.weightx = (i == msgPanelIndex ? 1.0 : 0.0);
            add(panels[i], gbc);
        }




    }

    private static JPanel createComponent(final JComponent innerComp){
        JPanel ret = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.LINE_END;
        gbc.insets = new Insets(1, 7, 1, 7);
        gbc.weightx = 1.0;
        ret.add(innerComp, gbc);
        ret.setBorder(BorderFactory.createLoweredBevelBorder());

        return ret;
    }



    /**
     * Set the message on the right side of the status bar.
     *
     * @param txt
     *          The text to display on the status bar.
     */
    public void setMessage(final String txt){
        message.setText(txt);
    }

    /**
     * Store whatever message is currently being displayed.
     * The next time resetMessage() is called this stored message will be put back.
     *
     * Use this if you are doing a lot of different things that you want to keep the user
     * aprised of via this status bar, and when your done you call reset and the message
     * will go back to what it was originally.
     *
     */
    public void captureMessage(){
        savedText = message.getText();
    }

    /**
     * Reset the message on the status bar to whatever it was the last time captureMessage()
     * was called.
     *
     */
    public void resetMessage(){
        if(savedText != null) {
			message.setText(savedText);
		}
    }

    /**
     * Clear the message on the status bar.
     * Any clear any captured messages.
     */
    public void clearMessage(){
        message.setText("");
        captureMessage();
    }

}
